package com.nbcb.fund.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nbcb.fund.common.util.MD5Util;
import com.nbcb.fund.entity.code.FundCode;
import com.nbcb.fund.entity.dto.FundResult;
import com.nbcb.fund.entity.param.LoginParam;
import com.nbcb.fund.entity.param.RegisterParam;
import com.nbcb.fund.entity.po.FundUserLoginTokenPo;
import com.nbcb.fund.entity.po.FundUserPo;
import com.nbcb.fund.service.FundUserLoginTokenService;
import com.nbcb.fund.service.FundUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 九分石人
 */
@Api
@RestController
@RequestMapping("user")
@Slf4j
public class UserController {

    @Autowired
    FundUserService fundUserService;

    @Autowired
    FundUserLoginTokenService fundUserLoginTokenService;

    @Autowired
    HttpServletResponse response;

    @Autowired
    HttpServletRequest request;
    @Autowired
    ObjectMapper objectMapper;


    @ApiOperation("登录")
    @PostMapping("login")
    public FundResult login(@RequestBody LoginParam loginParam) throws JsonProcessingException {
        String userId = loginParam.getUserId();
        FundUserPo fundUserPo = fundUserService.getByUserId(userId);
        if (null == fundUserPo) {
            return FundResult.bizfail(FundCode.NO_USER);
        }
        String userPasswordByDB = fundUserPo.getUserPassword();
        String userPassword = loginParam.getUserPassword();
        String pwd = MD5Util.MD5(userPassword);
        if (pwd.equals(userPasswordByDB)) {
            //这里是新表
            FundUserLoginTokenPo fundUserLoginToken = fundUserLoginTokenService.generateToken(fundUserPo);
            //密码相同，登录成功
            Cookie cookie = new Cookie("token", fundUserLoginToken.getUserToken());
            cookie.setMaxAge(3600 * 12);
            cookie.setDomain(request.getServerName());
            cookie.setHttpOnly(false);
            response.addCookie(cookie);
            return FundResult.success(fundUserLoginToken.getUserToken());
        } else {
            //密码不同，登录失败
            return FundResult.bizfail(FundCode.ERROR_PWD);
        }
    }

    @ApiOperation("注册")
    @PostMapping("register")
    public FundResult register(@RequestBody RegisterParam registerParam) {
        if (null == registerParam) {
            return FundResult.bizfail(FundCode.BIZFAIL);
        }
        if (!StringUtils.hasLength(registerParam.getUserId())) {
            return FundResult.bizfail("工号不能为空");
        }
        if (!StringUtils.hasLength(registerParam.getUserName())) {
            return FundResult.bizfail("用户名不能为空");
        }
        if (!StringUtils.hasLength(registerParam.getUserPassword())) {
            return FundResult.bizfail("密码不能为空");
        }
        if (!StringUtils.hasLength(registerParam.getUserTelephone())) {
            return FundResult.bizfail("手机号不能为空");
        }
        FundUserPo byUserId = fundUserService.getByUserId(registerParam.getUserId());
        if (byUserId != null) {
            return FundResult.bizfail("该用户已存在");
        }
        int i = fundUserService.save(registerParam);
        return FundResult.success(i);
    }


}
